package com.lewis.system.controller;

import com.lewis.core.annotation.Log;
import com.lewis.core.base.controller.BaseController;
import com.lewis.core.base.domain.BaseResult;
import com.lewis.core.base.domain.entity.SysDictData;
import com.lewis.core.base.page.TableDataInfo;
import com.lewis.core.enums.BusinessType;
import com.lewis.core.utils.SecurityUtil;
import com.lewis.core.utils.StringUtil;
import com.lewis.core.utils.office.excel.ExcelUtil;
import com.lewis.system.service.ISysDictDataService;
import com.lewis.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author Lewis
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Resource
    private ISysDictDataService sysDictDataService;

    @Resource
    private ISysDictTypeService sysDictTypeService;

    @ApiOperation(value = "获取字典列表", notes = "获取字典列表")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        return getTableData(sysDictDataService.selectDictDataList(dictData));
    }

    @ApiOperation(value = "导出字典列表", notes = "导出字典列表")
    @Log(title = "字典数据" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public BaseResult export(SysDictData dictData) {
        List<SysDictData> list = sysDictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    @ApiOperation(value = "查询字典数据详细", notes = "查询字典数据详细")
    @GetMapping(value = "/{ids}")
    public BaseResult getInfo(@PathVariable Long ids) {
        return ok().setData(sysDictDataService.getById(ids));
    }

    @ApiOperation(value = "根据字典类型查询字典数据信息", notes = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public BaseResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = sysDictTypeService.selectDictDataByType(dictType);
        if (StringUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return ok().setData(data);
    }

    @ApiOperation(value = "新增字典类型", notes = "新增字典类型")
    @Log(title = "字典数据" , businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtil.getUsername());
        return getResult(sysDictDataService.insert(dict));
    }

    @ApiOperation(value = "修改保存字典类型", notes = "修改保存字典类型")
    @Log(title = "字典数据" , businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtil.getUsername());
        return getResult(sysDictDataService.updateDictData(dict));
    }

    @ApiOperation(value = "删除字典类型", notes = "删除字典类型")
    @Log(title = "字典类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public BaseResult remove(@PathVariable Long[] ids) {
        sysDictDataService.deleteDictDataByIds(ids);
        return ok();
    }
}
